﻿namespace AC.ExtendedRenderer.Toolkit
{
    using AC.ExtendedRenderer.Toolkit.Drawing;
    using ComponentFactory.Krypton.Toolkit;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Text;
    using System.Windows.Forms;

    public class KryptonComboBoxEnhanced : ComponentFactory.Krypton.Toolkit.KryptonComboBox
    {
        private bool _enabled = true;
        private Color _gradientEndColor = Color.Gray;
        private Color _gradientStartColor = Color.White;
        private IPalette _palette;
        private PaletteRedirect _paletteRedirect;
        private bool _persistentColors;
        private bool _useStyledColors;
        private IContainer components;

        public KryptonComboBoxEnhanced()
        {
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
            base.UpdateStyles();
            base.ComboBox.DrawMode = DrawMode.OwnerDrawVariable;
            base.ItemHeight += 2;
            base.ComboBox.DrawItem += new DrawItemEventHandler(this.OnDItem);
            if (this._palette != null)
            {
                this._palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            KryptonManager.GlobalPaletteChanged += new System.EventHandler(this.ThisGlobalPaletteChanged);
            this._palette = KryptonManager.CurrentGlobalPalette;
            this._paletteRedirect = new PaletteRedirect(this._palette);
            this.InitializeComponent();
            if (!this.Enabled)
            {
                this.Enabled = true;
                base.ComboBox.Enabled = false;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
        }

        protected void ODrawItem(DrawItemEventArgs e)
        {
            Rectangle bounds = e.Bounds;
            Graphics g = e.Graphics;
            if (e.Index != -1)
            {
                try
                {
                    e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
                }
                catch (Exception exception)
                {
                    Console.Write(exception.Message);
                }
                if (this._palette == null)
                {
                    EventArgs args = new EventArgs();
                    this.ThisGlobalPaletteChanged(this, args);
                }
                if (!this._persistentColors)
                {
                    if (this._useStyledColors)
                    {
                        this._gradientStartColor = Color.FromArgb(0xff, 0xf6, 0xd7);
                        this._gradientEndColor = Color.FromArgb(0xff, 0xd5, 0x4d);
                    }
                    else
                    {
                        this._gradientStartColor = this._palette.ColorTable.StatusStripGradientBegin;
                        this._gradientEndColor = this._palette.ColorTable.OverflowButtonGradientEnd;
                    }
                }
                Color darkColor = this._gradientStartColor;
                Color lightColor = this._gradientEndColor;
                Color statusStripText = this._palette.ColorTable.StatusStripText;
                Font font = e.Font;
                if (font == null)
                {
                    font = this.Font;
                }
                e.DrawBackground();
                string s = (string) base.Items[e.Index];
                if (!this.Enabled)
                {
                    e.Graphics.DrawString(s, font, new SolidBrush(SystemColors.GrayText), e.Bounds);
                }
                else
                {
                    e.Graphics.DrawString(s, font, new SolidBrush(this.ForeColor), e.Bounds);
                }
                if ((e.State & DrawItemState.Selected) != DrawItemState.None)
                {
                    DrawingMethods.DrawGradient(g, e.Bounds, darkColor, lightColor, 90f, false, Color.White, 0f);
                    e.Graphics.DrawString(s, font, new SolidBrush(statusStripText), e.Bounds);
                    e.DrawFocusRectangle();
                }
                e.DrawFocusRectangle();
            }
        }

        private void OnDItem(object Sender, DrawItemEventArgs e)
        {
            this.ODrawItem(e);
        }

        private void OnPalettePaint(object sender, PaletteLayoutEventArgs e)
        {
            base.Invalidate();
        }

        private void ThisGlobalPaletteChanged(object sender, EventArgs e)
        {
            if (this._palette != null)
            {
                this._palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            this._palette = KryptonManager.CurrentGlobalPalette;
            this._paletteRedirect.Target = this._palette;
            if (this._palette != null)
            {
                this._palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
                if (!this._persistentColors)
                {
                    if (this._useStyledColors)
                    {
                        this._gradientStartColor = Color.FromArgb(0xff, 0xf6, 0xd7);
                        this._gradientEndColor = Color.FromArgb(0xff, 0xd5, 0x4d);
                    }
                    else
                    {
                        this._gradientStartColor = this._palette.ColorTable.StatusStripGradientBegin;
                        this._gradientEndColor = this._palette.ColorTable.OverflowButtonGradientEnd;
                    }
                }
            }
            base.Invalidate();
        }

        public bool Enabled
        {
            get
            {
                return this._enabled;
            }
            set
            {
                this._enabled = value;
                base.ComboBox.Enabled = value;
                base.Invalidate();
            }
        }

        [DefaultValue("Color.Gray"), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(true), Category("Appearance-Extended")]
        public Color GradientEndColor
        {
            get
            {
                return this._gradientEndColor;
            }
            set
            {
                this._gradientEndColor = value;
                base.Invalidate();
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue("Color.White"), Browsable(true), Category("Appearance-Extended")]
        public Color GradientStartColor
        {
            get
            {
                return this._gradientStartColor;
            }
            set
            {
                this._gradientStartColor = value;
                base.Invalidate();
            }
        }

        [Browsable(true), DefaultValue("False"), Category("Appearance-Extended")]
        public bool PersistentColors
        {
            get
            {
                return this._persistentColors;
            }
            set
            {
                this._persistentColors = value;
            }
        }

        [DefaultValue("False"), Category("Appearance-Extended"), Browsable(true)]
        public bool UseStyledColors
        {
            get
            {
                return this._useStyledColors;
            }
            set
            {
                this._useStyledColors = value;
            }
        }
    }
}

